#include "skmodelconstraint.h"
#include <QString>
#include <string>
SkModelConstraint::SkModelConstraint(SketchModelWrapper* modelo,QObject *parent) :
QAbstractTableModel(parent)
{
	model = modelo;
	lista = new std::list<ConstraintData>(model->getConstraintData());
}
int SkModelConstraint::rowCount( const QModelIndex & parent  ) const {
	Q_UNUSED(parent);
	return lista->size();

}
int SkModelConstraint::columnCount ( const QModelIndex & parent ) const{
	Q_UNUSED(parent);
	return 2;
}
QVariant SkModelConstraint::data ( const QModelIndex & index, int role ) const{
	if (!index.isValid())
		return QVariant();
	if(role==Qt::DisplayRole || role==Qt::EditRole){

		if(index.column()==0){
			int i=0;
			for(std::list<ConstraintData>::iterator it= lista->begin(); it!=lista->end(); it++){
				if(index.row()==i){
					return it->id;
				}
				i++;
			}
		}else if(index.column()==1){
			int i=0;
			for(std::list<ConstraintData>::iterator it= lista->begin(); it!=lista->end(); it++){
				if(index.row()==i){
					return QString(it->type.c_str());
				}
				i++;
			}
		}

	}
	if(role==Qt::ToolTipRole){
		QString ss;
		int i=0;
		for(std::list<ConstraintData>::iterator it= lista->begin(); it!=lista->end(); it++){
			if(index.row()==i){

		        return  QString( "ID:\t%1,\r\n K:\t%2,\r\n L:\t%3,\r\n M:\t%4,\r\n N:\t%5,\r\nParamID:\t%6 " )
		                    .arg(it->id ).arg(it->idK).arg(it->idL)
		                    .arg(it->idM).arg(it->idN).arg(it->paramId);
				break;
			}
			i++;
		}
	}

	return QVariant();

}
bool SkModelConstraint::setData ( const QModelIndex & index, const QVariant & value, int role  ) {
	Q_UNUSED(index);
	Q_UNUSED(value);
	Q_UNUSED(role);
	return false;
}

QVariant SkModelConstraint::headerData(int section, Qt::Orientation orientation,int role ) const{

	if (role != Qt::DisplayRole) return QVariant();

	if (orientation == Qt::Horizontal){

		switch(section){
		case 0:
			return QString("id");
		case 1:
			return QString("name");
		}

	}

	return QString("%1").arg(section);


}

Qt::ItemFlags SkModelConstraint::flags(const QModelIndex &index) const{
	if (!index.isValid())
		return Qt::ItemIsEnabled;

	switch(index.column()){
	case 0:
		return QAbstractItemModel::flags(index);
	case 1:
		return QAbstractItemModel::flags(index);

	}

	return NULL;
}

void SkModelConstraint::resetList(){
	delete lista;

	lista = new std::list<ConstraintData>(model->getConstraintData());
	reset();
}

SkModelConstraint::~SkModelConstraint(){
	delete lista;
}
